package net.sourceforge.simcpux.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiConfirmOrderInfo;
import com.jiukuaidao.zhulin.module.CommContract;
import com.jiukuaidao.zhulin.module.CommPresenter;
import com.jiukuaidao.zhulin.module.order.OrderDetailActivity;
import com.jiukuaidao.zhulin.module.pay.wechat.WXPayContract;
import com.jiukuaidao.zhulin.module.pay.wechat.util.WXKeys;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/24
 * Description : 微信支付结果页面
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler, CommContract.FinishPayView {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    private String mOrderNo;
    private String mSataus = "1";
    private CommPresenter mCommPresenter;
    private WXPayContract.View mView;

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getEnterDirection() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return 0;
    }

    @Override
    protected void handleBundle(Bundle bundle) {

    }

    @Override
    protected void init() {
        api = WXAPIFactory.createWXAPI(this, WXKeys.APP_ID);
        api.handleIntent(getIntent(), this);
        EventBus.getDefault().register(this);
        mCommPresenter = new CommPresenter(TaskProvider.provideTasksRepository(this), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        //Log.d(TAG, "onPayFinish, errCode = " + resp.errCod
        Bundle bundle = new Bundle();
        bundle.putString("orderNumber", mOrderNo);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_COMM:  // 如果是没有调起微信客户端
                    ToastUtil.show("调用失败", Toast.LENGTH_SHORT);
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消支付
                    ToastUtil.show("取消支付", Toast.LENGTH_SHORT);
                    break;
                case BaseResp.ErrCode.ERR_OK://支付成功
                    ToastUtil.show("支付成功", Toast.LENGTH_SHORT);
                    mCommPresenter.getFinishPay();
                    break;

            }
        }
        if (OrderDetailActivity.mInstance == null) {
            intentJump(OrderDetailActivity.class, bundle);
        } else {
            mView.refreshInfo();
        }
        WXPayEntryActivity.this.finish();

    }

    @Subscribe
    public void onEventOrderInfo(ApiConfirmOrderInfo orderInfo) {
        mOrderNo = orderInfo.orderNo;
    }

    @Override
    public String getOrderNo() {
        return mOrderNo;
    }

    @Override
    public String getStatus() {
        return mSataus;
    }
}